<!DOCTYPE html>
<html lang="zh">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>客户订单查询</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f5f5f5;
        }

        .container {
            background-color: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
        }

        .search-form {
            margin-bottom: 20px;
            text-align: center;
        }

        .search-form input {
            padding: 8px;
            width: 200px;
            border: 1px solid #ddd;
            border-radius: 4px;
        }

        .search-form button {
            padding: 8px 16px;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }

        .search-form button:hover {
            background-color: #45a049;
        }

        .stats-summary {
            margin: 20px 0;
            padding: 15px;
            background-color: #f8f9fa;
            border-radius: 4px;
        }

        .orders-table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
        }

        .orders-table th,
        .orders-table td {
            padding: 12px;
            text-align: left;
            border-bottom: 1px solid #ddd;
        }

        .orders-table th {
            background-color: #f8f9fa;
        }

        .no-results {
            text-align: center;
            padding: 20px;
            color: #666;
        }
    </style>
</head>

<body>
<div class="container">
    <h1 style="text-align: center;">客户订单查询</h1>
    {% if error %}
    <div class="error-message"
         style="color: #dc3545; text-align: center; margin: 10px 0; padding: 10px; background-color: #f8d7da; border-radius: 4px;">
        {{ error }}
    </div>
    {% endif %}
    {% if customer_code or phone %}
    {% if orders %}
    <div class="stats-summary">
        <h2>统计结果汇总</h2>
        <p>订单总数: {{ stats.total_orders }} 单</p>
        <p>销售总额: ￥{{ "%.2f"|format(stats.total_amount) }}</p>
        <p>平均单价: ￥{{ "%.2f"|format(stats.total_amount/stats.total_orders) if stats.total_orders > 0 else "0.00" }}
        </p>
    </div>

    <h2>订单明细</h2>
    <table class="orders-table">
        <thead>
        <tr>
            <th>序号</th>
            <th>订单号</th>
            <th>消费渠道</th>
            <th>商品名称</th>
            <th>实付金额</th>
            <th>交易时间</th>
        </tr>
        </thead>
        <tbody>
        {% for order in orders %}
        <tr>
            <td>{{ loop.index }}</td>
            <td>{{ order.tradeno }}</td>
            <td>{{ order.shopname }}</td>
            <td style="white-space: pre-line">{{ order.goodsname }}</td>
            <td>￥{{ "%.2f"|format(order.realfee) }}</td>
            <td>{{ order.tradetime.strftime('%Y-%m-%d %H:%M:%S') }}</td>
        </tr>
        {% endfor %}
        </tbody>
    </table>
    {% else %}
    <div class="no-results">
        {% if is_phone_query %}
        <p>未找到手机号 {{ phone }} 的订单信息</p>
        {% else %}
        <p>未找到客户 {{ customer_code }} 的订单信息</p>
        {% endif %}
    </div>
    {% endif %}
    {% endif %}
</div>
</body>

</html>